package com.grgbanking.it.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.grgbanking.it.common.json.JSONHelper;
import com.grgbanking.it.system.dao.impl.WXMenuDao;
import com.grgbanking.it.system.entity.WXMenu;
import com.grgbanking.it.system.service.IWXMenuService;
import com.grgbanking.it.wx.common.creator.WXMenuCreator;

/**
 * 微信菜单管理
 * 
 * WXMenuService
 * 
 * @author 潘广伟
 * @Email p_3er@qq.com
 * @Date 2015-1-8 上午9:50:57
 * 
 * @version 1.0.0
 *
 */
@Service
public class WXMenuService implements IWXMenuService {
	@Autowired
	private WXMenuDao menuDao;
	@Autowired
	private WXMenuCreator menuCreator;

	@Override
	public void saveOrUpdate(WXMenu menu) throws Exception {
		menu.setState(1);
		menuDao.saveOrUpdate(menu);
	}

	@Override
	public String findToTree(Integer agentId) throws Exception {
		List<WXMenu> menus = this.findAllMenus(agentId, false);
		return JSONHelper.converList(menus);
	}
	
	/**
	 * 获取所有菜单
	 * findAllMenus
	 * 
	 * @param agentId
	 * @param isValid
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public List<WXMenu> findAllMenus(Integer agentId,boolean isValid){
		List<WXMenu> menus = this.getParent(agentId, isValid);
		
		for(WXMenu menu : menus){
			List<WXMenu> children = this.getChildren(menu.getId(), isValid);
			if(children.size() > 0){
				menu.setChildren(children);
				menu.setLeaf(false);
			}
		}
		
		return  menus;
	}
	
	/**
	 * 获取子菜单
	 * getChildren
	 * 
	 * @param parent 父菜单ID
	 * @param isValid 是否全部为有效数据
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	private List<WXMenu> getChildren(Integer parent,boolean isValid){
		StringBuffer hql = new StringBuffer("from WXMenu t where t.parent=");
		hql.append(parent);
		if(isValid){
			hql.append(" and t.state=1");
		}
		
		return menuDao.findByHql(hql.toString());
	}
	/**
	 * 获取父菜单
	 * findParent
	 * 
	 * @param agentId 应用编号
	 * @param isValid 是否全部为有效数据
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	private List<WXMenu> getParent(Integer agentId,boolean isValid){
		StringBuffer hql = new StringBuffer("from WXMenu t where (t.parent is null or t.parent=0)");
		
		if(agentId != null){
			hql.append(" and t.agentId=").append(agentId);
		}
		
		if(isValid){
			hql.append(" and t.state=1");
		}
		
		return menuDao.findByHql(hql.toString());
	}
 
	@Override
	public String findByIdToJson(Integer id) throws Exception {
		return JSONHelper.convertObjectForForm(menuDao.findById(id));
	}
	
	/**
	 * 删除
	 * delete
	 * 
	 * @param id
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public void delete(Integer id) throws Exception {
		menuDao.virtualDelete(id);
	}
	
	/**
	 * 获取所有有效父菜单
	 * findParentMenus
	 * 
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public String findParentMenus(Integer agentId) throws Exception {
		List<WXMenu> menus = this.getParent(agentId, true);
		
		return JSONHelper.converList(menus);
	}
	
	/**
	 * 更新应用菜单到微信
	 * updateMemuToWX
	 * 
	 * @param agentId
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public void updateMemuToWX(Integer agentId) throws Exception {
		List<WXMenu> menus = this.findAllMenus(agentId, true);
		
		String json = JSONHelper.converMenu(menus);
		menuCreator.delete(agentId);
		menuCreator.create(agentId, json);
	}
	
	

}
 